/**
 *!
 * \file        b_drv_chipflash.c
 * \version     v0.0.1
 * \date        2021/5/10
 * \author      Rain
 *******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Bean
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************
 */

/*Includes ----------------------------------------------*/
#include "b_drv_chipflash.h"
/**
 * \addtogroup B_DRIVER
 * \{
 */

/**
 * \addtogroup 24CXX
 * \{
 */

/**
 * \defgroup 24CXX_Private_TypesDefinitions
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup 24CXX_Private_Defines
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup 24CXX_Private_Macros
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup 24CXX_Private_Variables
 * \{
 */
const static bchipflash_hal_if_t bchipflash_hal_if_table[] = HAL_CHIPFLASH_IF;
bchipflash_Driver_t
    bchipflash_Driver[sizeof(bchipflash_hal_if_table) / sizeof(bchipflash_hal_if_t)];
/**
 * \}
 */

/**
 * \defgroup 24CXX_Private_FunctionPrototypes
 *
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup 24CXX_Private_Functions
 * \{
 */

/************************************************************************************************************driver
 * interface*******/

static int _bchipflash_Write(bchipflash_Driver_t *pdrv, uint32_t off, uint8_t *pbuf, uint16_t len)
{
    bchipflash_hal_if_t *hal_if = pdrv->_hal_if;

    b_hal_onchip_write_page(hal_if->offset_address + off, len, pbuf);

    return len;
}

static int _bchipflash_Read(bchipflash_Driver_t *pdrv, uint32_t off, uint8_t *pbuf, uint16_t len)
{
    bchipflash_hal_if_t *hal_if = pdrv->_hal_if;

    b_hal_onchip_read_page(hal_if->offset_address + off, len, pbuf);

    return len;
}
static int _bchipflash_Ctl(bchipflash_Driver_t *pdrv, uint8_t cmd, void *param)
{
    int                  retval = -1;
    bchipflash_hal_if_t *hal_if = pdrv->_hal_if;
    switch (cmd)
    {
        case bCMD_ERASE_SECTOR:
        {
            if (param)
            {
                bCMD_Erase_t *perase_param = (bCMD_Erase_t *)param;

                b_hal_onchip_earse_sector(hal_if->offset_address + perase_param->addr,
                                          b_hal_onchip_get_sector_size() * param->num);
                retval = 0;
            }
        }
        break;
        case bCMD_GET_SECTOR_SIZE:
        {
            if (param)
            {
                ((uint32_t *)param)[0] = b_hal_onchip_get_sector_size();
                retval                 = 0;
            }
        }
        break;
        case bCMD_GET_SECTOR_COUNT:
        {
            if (param)
            {
                ((uint32_t *)param)[0] = ((bchipflash_hal_if_t *)pdrv->_hal_if)->sector_count;
                retval                 = 0;
            }
        }
        break;
    }
    return retval;
}
/**
 * \}
 */

/**
 * \addtogroup 24CXX_Exported_Functions
 * \{
 */
int bchipflash_Init()
{
    uint8_t i = 0, num_drv = sizeof(bchipflash_hal_if_table) / sizeof(bchipflash_hal_if_t);
    for (i = 0; i < num_drv; i++)
    {
        bchipflash_Driver[i]._hal_if = (void *)&bchipflash_hal_if_table[i];
        bchipflash_Driver[i].status  = 0;
        bchipflash_Driver[i].close   = NULL;
        bchipflash_Driver[i].read    = _bchipflash_Read;
        bchipflash_Driver[i].ctl     = _bchipflash_Ctl;
        bchipflash_Driver[i].open    = NULL;
        bchipflash_Driver[i].write   = _bchipflash_Write;
    }
    return 0;
}

bDRIVER_REG_INIT(bchipflash_Init);

/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */

/************************ Copyright (c) 2020 Bean *****END OF FILE****/
